import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		user: {},
		hasLogin: false, //判定是否登录
		isRealName:false, //实名认证状态
		userInfo: {},
		num: 0,
	},
	getters: {
		getUser: state => state.user,
		getRealName: state => state.isRealName,
		getHasLogin: state => state.hasLogin,
	},
	mutations: {
		SET_USER: (state, value) => {
			state.user = value;
		},
		SET_REAL_NAME: (state, value) => {
			state.isRealName = value;
		},
		SET_HAS_LOGIN: (state, value) => {
			state.hasLogin = value;
		},
	},
	actions: {
		// 设置全局变量方法，用于异步调用，调用方式：this.$store.dispatch('setCollapse', value) 
		setUser({ commit }) {
			return new Promise((resolve, reject) => {
				getUser().then((res) => {
					if (res && res.code == 200) {
						let user = {
							userName: res.data.data.userName,
							account: res.data.data.account,
							lastLoginTime: res.data.data.lastLoginTime,
							adminLevel: res.data.data.adminLevel,
							roleType: res.data.data.roleType,
							roleName: res.data.data.roleName,
							belongId: res.data.data.belongId,
							villageName: res.data.data.villageName
						};
						commit('SET_USER', user);
					}
					resolve(res);
				})
			})
		},
	}
})

export default store
